package com.ossjk.qlh.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ossjk.qlh.system.dto.UserDto;
import com.ossjk.qlh.system.entity.User;
import com.ossjk.qlh.system.entity.UserRole;
import com.ossjk.qlh.system.mapper.UserMapper;
import com.ossjk.qlh.system.service.IUserRoleService;
import com.ossjk.qlh.system.service.IUserService;

/**
 * Copyright 2020-12-14 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.system.service.impl
 * @ClassName: UserServiceImpl
 * @Description: 用户表-服务实现类
 * @author: chair
 * @date: 2020-12-14 22:06:31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	@Autowired
	private IUserRoleService iUserRoleService;

	@Override
	public boolean saveDto(UserDto dto) {
		boolean result = save(dto);
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("uid", dto.getId());
		iUserRoleService.remove(queryWrapper);
		UserRole entity = new UserRole();
		entity.setUid(dto.getId());
		entity.setRid(dto.getRoleId());
		iUserRoleService.save(entity);
		return result;
	}

	@Override
	public boolean updateDtoById(UserDto dto) {
		boolean result = updateById(dto);
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("uid", dto.getId());
		iUserRoleService.remove(queryWrapper);
		UserRole entity = new UserRole();
		entity.setUid(dto.getId());
		entity.setRid(dto.getRoleId());
		iUserRoleService.save(entity);
		return result;
	}

	@Override
	public Page pageVo(Page page, Wrapper wrapper) {
		return this.baseMapper.selectPageVo(page, wrapper);
	}

}
